﻿
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[medianamik_DeleteHistory]') AND type in (N'U'))
	BEGIN

	CREATE TABLE [dbo].[medianamik_DeleteHistory](
		[Id] [bigint] IDENTITY(1,1) NOT NULL,
		[NodeId] [uniqueidentifier] NOT NULL,
		[TypeId] [uniqueidentifier] NOT NULL,
		[DeletedDate] [datetime] NOT NULL,
	 CONSTRAINT [PK_medianamik_DeleteHistory] PRIMARY KEY CLUSTERED 
	(
		[Id] ASC
	)
	) ON [PRIMARY]

	ALTER TABLE [dbo].[medianamik_DeleteHistory] ADD  CONSTRAINT [DF_medianamik_DeleteHistory_DeletedDate]  DEFAULT (getdate()) FOR [DeletedDate]
END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[vw_medianamik_DeleteHistory]'))
BEGIN
	declare @sql nvarchar(4000)
	SET @sql = 'CREATE VIEW [dbo].[vw_medianamik_DeleteHistory]
	AS
	SELECT     dbo.medianamik_DeleteHistory.NodeId, dbo.medianamik_Type.Name, dbo.medianamik_DeleteHistory.DeletedDate
	FROM         dbo.medianamik_DeleteHistory INNER JOIN
						  dbo.medianamik_Type ON dbo.medianamik_DeleteHistory.TypeId = dbo.medianamik_Type.TypeId'
						  
	exec sp_executesql @sql

END

